package redis

import (
	"time"
)

// Set 插入
func Set(key string, value string) error {
	return redisClient.Set(ctx, key, value, 0).Err()
}

func GetSet(key string, value string) error {
	return redisClient.GetSet(ctx, key, value).Err()
}

// SetTime 插入（带过期时间）
func SetTime(key string, value string, expiration time.Duration) error {
	return redisClient.SetEX(ctx, key, value, expiration).Err()
}

// RefTime 刷新过期时间
func RefTime(key string, expiration time.Duration) error {
	return redisClient.Expire(ctx, key, expiration).Err()
}

// Get 获取
func Get(key string) (val string) {
	if Exists(key) {
		data, _ := redisClient.Get(ctx, key).Bytes()
		return string(data)
	}
	return ""
}

func Exists(key string) bool {
	count, _ := redisClient.Exists(ctx, key).Result()
	return count > 0
}

// Get 获取(通配符)
func ScanKeys(key string) (keys []string, err error) {
	cursor := uint64(0)
	count := int64(10000)
	keys, cursor, err = redisClient.Scan(ctx, cursor, key, count).Result()
	return
}

// Get 获取
func ScanVals(keyM string) (vals []string, err error) {
	keys, err := ScanKeys(keyM)
	if err != nil {
		return nil, err
	}
	for _, key := range keys {
		val := Get(key)
		vals = append(vals, val)
	}
	return
}

// Del 删除
func Del(key ...string) {
	redisClient.Del(ctx, key...)
}
